package com.sykj.manage.web;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Size;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.mysql.fabric.xmlrpc.base.Data;
import com.sykj.manage.service.BisShopService;
import com.sykj.manage.service.BisSmsRecordMService;
import com.sykj.manage.model.BisShop;
import com.sykj.manage.model.BisSmsRecordM;
import com.sykj.util.JavaSmsApi1;
import com.sykj.util.MYSConstant;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;
/**
 * 短信记录表控制类
 * @author 蔡聪
 *	2018年9月10日下午3:17:40
 */
@Controller
@RequestMapping("/bissmsrecord")
public class BisSmsRecordController {
	
	/*static final String UPLOAD_PATH = "/static/manage/images/";*/
	/**
	 * 调用短信记录信息业务层类
	 */
	@Autowired
	BisSmsRecordMService bisSmsRecordService;
	/**
	 * 显示全部短信记录实现方法
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	@ResponseBody
	public ManageBody bisshopShow(BisSmsRecordM bisSmsRecord,Integer offset,Integer pageSize){
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize),pageSize==null?10:pageSize,true);
		List<BisSmsRecordM> allBisSmsRecord = bisSmsRecordService.allBisSmsRecord(bisSmsRecord);
		PageInfo<BisSmsRecordM>pageInfo=new PageInfo<BisSmsRecordM>(allBisSmsRecord);
		return ManageBody.getData((int)pageInfo.getTotal(),pageInfo.getList(),"");
	}
	
	@RequestMapping("/delrecord")
	@ResponseBody
	public Object delParams(@RequestParam("record_id")String[] record_id) {
		int cnt = bisSmsRecordService.delRecord(record_id);
		return cnt;
	}
	@RequestMapping("/add")
	@ResponseBody
	public Object add(BisSmsRecordM bisSmsRecordM) throws IOException {
		String[] Mobile = bisSmsRecordM.getMobile().split(",");
		JavaSmsApi1 j = new JavaSmsApi1();
		String content = bisSmsRecordM.getContent();
		int add = 0;
		for (String string : Mobile) {
			String sendSms = j.sendSms1(string,content);
			String replace = UUID.randomUUID().toString().replace("-", "");
			bisSmsRecordM.setRecord_id(replace);
			bisSmsRecordM.setMobile(string);
			Gson gson = new Gson();
			Map<String, Object> map = gson.fromJson(sendSms, HashMap.class);
			if (map.get("respCode").equals("00000")) {
				bisSmsRecordM.setStatus("1");
				add = bisSmsRecordService.add(bisSmsRecordM);
			}else {
				bisSmsRecordM.setStatus("2");
				String object = (String) map.get("respDesc");
				bisSmsRecordM.setFailure_cause(object);
				add = bisSmsRecordService.add(bisSmsRecordM);
			}
			
		}
		return add;
	}
}
